//
//  EncryptionTool.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/7/18.
//  Copyright © 2016年 Nick xu. All rights reserved.
//

import UIKit

class EncryptionTool: NSObject {
    
    class func encryptString(_ string: String) -> String {
        //string 转data 转bytes
        let data1:Data = string.data(using: String.Encoding.utf16LittleEndian)!
        var byteArray:[UInt8] = [UInt8]()
        for i in 0..<data1.count {
            var temp:UInt8 = 0
            (data1 as NSData).getBytes(&temp, range: NSRange(location: i,length:1 ))
            byteArray.append(temp)
            }
        // bytes翻转
        let bytes:[UInt8] = byteArray.reversed()
        //bytes转data  data base64加密  加密后data转string
        let data:Data = Data(bytes: UnsafePointer<UInt8>(bytes), count: bytes.count)
        
        let dataBase64:Data = data.base64EncodedData(options: NSData.Base64EncodingOptions.lineLength76Characters)
        
        var stringBase64:String =  String(data: dataBase64, encoding: String.Encoding.utf8)!
        
        //长度小于10 添加字符串
        if strlen(stringBase64) < 10 {
            stringBase64 += "YeT+=fue"
            }
        return stringBase64
        }

}
